//
//  NeedHallViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/4.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "NeedHallViewModel.h"
#import "GoodsTopSelectView.h"
#import "HomeHotPictureTableViewCell.h"
#import "HomeHotOrderTableViewCell.h"
#import "AdressAndSortingView.h"
#import "ScreeningView.h"
#import "NeedButSelectView.h"

@interface NeedHallViewModel ()<UITableViewDelegate,UITableViewDataSource,GoodsTopDelegate>
{
    AdressAndSortingView * screeningView;
    ScreeningView * screenView;
    float lastContentOffset;
}
@property (nonatomic,strong) UIView * self_View;
@property (nonatomic,strong) UITableView * self_tableView;
@end

@implementation NeedHallViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.self_View = superView;
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(85);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(85);
        tableView.mj_header.hidden = YES;
        tableView.mj_footer.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        [superView addSubview:tableView];
        self.self_tableView = tableView;
        //
        GoodsTopSelectView * topView = [[GoodsTopSelectView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(34)) inSuperView:superView];
        topView.delegate = self;
        //
        [self createButCahnge];
        [self pushNeedView];
    }
    return self;
}

-(void)pushNeedView{
    //
    GLGWButton * pushBut = [[GLGWButton alloc] initWithTitle:@"发布\n求购" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor whiteColor] theCornerRadius:32.5 backgroundColr:kColor_Rule_Yellow inSuperView:self.self_View];
    pushBut.titleLabel.numberOfLines = 0;
    [pushBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.self_View.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(27));
        make.bottom.equalTo(self.self_tableView.mas_bottom).offset(-kHeightProportion_In_iPhoneTriploid(16));
        make.width.height.equalTo(@65);
    }];
    [pushBut addTarget:self action:@selector(pushNeedViewVC) forControlEvents:UIControlEventTouchUpInside];
}

-(void)pushNeedViewVC{
    if (self.delegate && [self.delegate respondsToSelector:@selector(pushOneNeedHallOrder)]) {
        [self.delegate pushOneNeedHallOrder];
    }
}

-(void)createButCahnge{
    NeedButSelectView * butSelectView = [[NeedButSelectView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(34), KScreenWidth, kHeightProportion_In_iPhoneTriploid(51)) inSuperView:self.self_View];
    butSelectView.Block_TopAction = ^(GLGWButton * _Nonnull s_but) {
        switch (s_but.tag) {
            case 131:{//普通
                
            }
                break;
            case 132:{//图片
                
            }
                break;
            case 133:{//极速
                
            }
                break;
            case 134:{//定金
                
            }
                break;
            case 135:{//全部求购
                
            }
                break;
                
            default:
                break;
        }
    };
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return kHeightProportion_In_iPhoneTriploid(163);
    }else{
        return kHeightProportion_In_iPhoneTriploid(190);
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    if (indexPath.row == 0) {
        HomeHotOrderTableViewCell * cell_hot = [HomeHotOrderTableViewCell cellWithTableView:tableView];
        cell = cell_hot;
    }else{
        HomeHotPictureTableViewCell * cell_Picture = [HomeHotPictureTableViewCell cellWithTableView:tableView];
        cell = cell_Picture;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(needPushDidSelectRowAtIndexPath:)]) {
        [self.delegate needPushDidSelectRowAtIndexPath:indexPath];
    }
}

//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGFloat offsetY = scrollView.contentOffset.y;
//    DLog(@"---------------  %f",offsetY);
//    CGPoint vel = [scrollView.panGestureRecognizer velocityInView:scrollView];
//    if (vel.y>0) {
//        DLog(@"向下");
//    }else{
//        DLog(@"👆");
//    }
//    //
//    if (lastContentOffset < scrollView.contentOffset.y){
//        NSLog(@"向上滚动");
//    }else{
//        NSLog(@"向下滚动");}
//}


//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
//    lastContentOffset = scrollView.contentOffset.y;
//}
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    if (lastContentOffset < scrollView.contentOffset.y){
////        NSLog(@"向上滚动");
//        if (self.butSelectView.height == 0) {
//            return;
//        }
//        [UIView animateWithDuration:0.5 animations:^{
//            self.butSelectView.height = 0;
//            self.self_tableView.y = self.self_tableView.y - kHeightProportion_In_iPhoneTriploid(51);
//            self.self_tableView.height = self.self_tableView.height + kHeightProportion_In_iPhoneTriploid(51);
//        }];
//    }else{
////        NSLog(@"向下滚动");
//        if (self.butSelectView.height == kHeightProportion_In_iPhoneTriploid(51)) {
//            return;
//        }
//        self.self_tableView.y = self.self_tableView.y + kHeightProportion_In_iPhoneTriploid(51);
//        self.self_tableView.height = self.self_tableView.height - kHeightProportion_In_iPhoneTriploid(51);
//        self.butSelectView.height = kHeightProportion_In_iPhoneTriploid(51);
//    }
//
//}
#pragma mark ----- GoodsTopDelegate
-(void)selectTopViewButTag:(NSInteger)tag_num{
    if (screenView || screeningView) {
        [screeningView dissmiss];
        [screenView dissmiss];
    }
    switch (tag_num) {
        case 180:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_AllAdress inSuperView:self.self_View];
        }
            break;
        case 181:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_Comprehensive inSuperView:self.self_View];
        }
            break;
        case 182:{
            CGFloat sc_height;
            if (iPhoneX) {
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(35) - 88 - kHeightProportion_In_iPhoneTriploid(34);
            }else{
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(35) - 64;
            }
            screenView = [[ScreeningView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, sc_height) inSuperView:self.self_View];
            
        }
            break;
            
        default:
            break;
    }
}


@end
